﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Lógica de interacción para EditProfile.xaml
    /// </summary>
    public partial class MyProfile : Window
    {
        User user=new User();
        Validate Val = new Validate();
        Client client = new Client();
        //Image_Manager image = new Image_Manager();

        public MyProfile()
        {
            InitializeComponent();
        }

        public MyProfile(User user)
        {
            InitializeComponent();
            this.user = user;
            Email.Text = user.Email;
            UserName.Text = user.Name;
            SecretQuestion.SelectedIndex = user.Question;
           
        }
        
        private void OldPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (OldPassword.Password.GetHashCode().ToString() != user.Password)
            {
                OldPassLabel.Content = "The password is incorrect";
                NewPassword.IsEnabled = false;
                RepitNewPassword.IsEnabled = false;
            }
            else
            {
                OldPassLabel.Content = "";
                NewPassword.IsEnabled = true;
                SecretQuestion.IsEnabled = true;
                Answer.IsEnabled = true;
                QuestionLabel.Content = "";
                AnswerLabel.Content = "";
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void NewPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (!(Val.ValidatePassword(NewPassword.Password)))
            {
                NewPassLabel.Content = "The password is not secure";
                RepitNewPassword.IsEnabled = false;
            }
            else
            {
                NewPassLabel.Content = "";
                RepitNewPassword.IsEnabled = true;
            }
        }

        private void RepitNewPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (NewPassword.Password != RepitNewPassword.Password)
                RepitNewPassLabel.Content = "The passwords are not equal";
            else
                RepitNewPassLabel.Content = "";
        }

        private void Email_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!(Val.ValidateEmail(Email.Text)))
                EmailLabel.Content = "The e-mail is incorrect";
            else
                EmailLabel.Content = "";
        }

        private void SecretQuestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void PrepareEdit()
        {
            string email=EmailLabel.Content.ToString();
            string password = OldPassword.Password.GetHashCode().ToString();
            string newPass = NewPassLabel.Content.ToString();
            string repitedNew = RepitNewPassLabel.Content.ToString();
            int questIndex = SecretQuestion.SelectedIndex;
            string quest = QuestionLabel.Content.ToString();
            string answer = Answer.Text.GetHashCode().ToString();
            string answerLabel = AnswerLabel.Content.ToString();
            EditMyProfile(email,password,newPass,repitedNew,questIndex,quest,answer,answerLabel);
        }

        private bool EditMyProfile(string email,string password,string newPass, string repitedNew, int questIndex, string quest, string answer, string answerLabel)
        {
            bool edited=false;
            if (String.IsNullOrEmpty(email))
                user.Email = Email.Text;
            if (password == user.Password)
            {
                if ((String.IsNullOrEmpty(newPass)) && (String.IsNullOrEmpty(repitedNew)))
                    user.Password = NewPassword.Password.GetHashCode().ToString();
                if (String.IsNullOrEmpty(answerLabel))
                {
                    if ((questIndex != user.Question)&&(String.IsNullOrEmpty(quest)))
                        user.Question = SecretQuestion.SelectedIndex;
                    user.Answer = answer;
                }
            }
            byte[] bytes = UserToByteArray(user);
            //client.Send("20 " + bytes.Length);
            if (client.Receive() == "01")
            {
                edited = true;
                MessageBox.Show("Edit successfully");
            }
            else
                MessageBox.Show("Error");
            return edited;
        }

        private void ChangePassword_Click(object sender, RoutedEventArgs e)
        {
            PrepareEdit();
        }

        private byte[] UserToByteArray(Object userReceived)
        {
            if (userReceived == null)
                return null;
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            MemoryStream memoryStream = new MemoryStream();
            binaryFormatter.Serialize(memoryStream, userReceived);
            return memoryStream.ToArray();
        }

        private void Answer_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ((SecretQuestion.SelectedIndex != user.Question)&&(Answer.Text==""))
                AnswerLabel.Content = "Please write an answer";
            else
                AnswerLabel.Content = "";
        }

        private void Name_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
        
    }
}
